package org.vectrics.user.dataweb;

import org.apache.log4j.Logger;
import org.vectrics.common.dom.MarshalUtils;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class UpdateUserAction extends UserWebAction {
    private transient static Logger log = Logger.getLogger(UpdateUserAction.class);

    public UpdateUserAction() {
        super();
    }

    /*
     * Find the data after the update.
     */
    public void getData(String command, Element requestData, Element document) {
        // User authUser =
        // UserAuthProvider.getInstance().getAuthorizedUser(request);
        log.debug("getData() - saving user");
        // Iterator iter = requestData.elementIterator();
        NodeList nodeList = requestData.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if (element.getNodeName().equals("user")) {
                    MarshalUtils marshaller = MarshalUtils.getInstance();

                    Person person = (Person) marshaller.elementToObject(element, Person.class);
                    if (log.isDebugEnabled()) {
                        log.debug("comitting user data to database");
                        log.debug("getData() - userName = " + person.getUser().getUserName());
                        log.debug("getData() - firstName = " + person.getFirstName());
                        log.debug("getData() - lastName = " + person.getLastName());
                    }
                    CoreServiceLocator.getPersonService().updatePerson(person);
                }
            }
        }

    }

}
